package net.controller;

import net.comment.Msg;
import net.pojo.User;
import net.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @Author admin
 * @Date 2019/5/23 8:17
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;



    @RequestMapping("/login")
    public String login(HttpSession session,User user,Model model){
        Integer sessionAttribute = (Integer) session.getAttribute("result");
        User login = userService.Login(user);
        if (login != null){
            if (sessionAttribute == 1){
                session.setAttribute("user",login);
                return "/index.jsp";
            }else if (sessionAttribute == 2){
                session.setAttribute("user",login);
                return "redirect:/links/findAll.do";
            }else if (sessionAttribute == 3){
                session.setAttribute("user",login);
                return "redirect:/comments/findPageByUid.do";
            }else if (sessionAttribute == 6){
                session.setAttribute("user",login);
                Integer pid = (Integer) session.getAttribute("pid");
                return "redirect:/artile/findById.do?pid="+pid;
            }
            return "";
        } else {
            model.addAttribute("msg", "用户名或密码错误!");
            return "/client/login.jsp";
        }
    }
    @RequestMapping("/exit")
    public String exit(HttpSession session){
        Integer sessionAttribute = (Integer) session.getAttribute("result");
        if (sessionAttribute == 1){
            session.invalidate();
            return "/index.jsp";
        }else if (sessionAttribute == 2){
            session.invalidate();
            return "redirect:/links/findAll.do";
        }else if (sessionAttribute == 3){
            session.invalidate();
            return "redirect:/comments/findPageByUid.do";
        }else if (sessionAttribute == 6){
            Integer pid = (Integer) session.getAttribute("pid");
            session.invalidate();
            return "redirect:/artile/findById.do?pid="+pid;
        }
            return "";
    }

    @RequestMapping("/regist")
    public String regist(User user,Model model){
        int i = userService.checkUserName(user.getUsername());
        if (i>0){
            model.addAttribute("msg","注册失败");
            return "/client/regist.jsp";
        }else {
            int regist = userService.regist(user);
            model.addAttribute("user",regist);
            return "/client/login.jsp";
        }
    }

    //在msg中校对用户是否已经注册
    @RequestMapping("/checkUser")
    @ResponseBody
    public Msg checkUser(User user){
        int resultCount = userService.checkUserName(user.getUsername());
        Msg msg = new Msg();
        if (resultCount==0){
            msg.setFlag(true);
        }else {
            msg.setFlag(false);
        }
        return msg;
    }

    @RequestMapping("/findById")
    public String findById(HttpSession session,Model model){
        User userById = (User) session.getAttribute("user");
        User userAll = userService.findById(userById.getUid());
        model.addAttribute("userAll",userAll);
        return "/client/personal.jsp";
    }

    @RequestMapping("/updatePassword")
    public String updatePassword(User user,String newpassword,HttpSession session){
        user.setPassword(newpassword);
        int i = userService.updatePassword(user);
        session.setAttribute("result",1);
        return "/client/login.jsp";
    }
}
